package com.ksc.storekeeper.client.view.impl;

import java.util.Comparator;
import java.util.List;

import com.github.gwtbootstrap.client.ui.ButtonCell;
import com.github.gwtbootstrap.client.ui.DataGrid;
import com.github.gwtbootstrap.client.ui.SimplePager;
import com.github.gwtbootstrap.client.ui.SimplePager.TextLocation;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.gwtplatform.mvp.client.ViewImpl;
import com.ksc.storekeeper.client.handlers.PlaceChangeHandler;
import com.ksc.storekeeper.client.i18n.StoreKeeperConstants;
import com.ksc.storekeeper.client.mvp.NameTokens;
import com.ksc.storekeeper.client.view.StoreItemsView;
import com.ksc.storekeeper.shared.dto.ProductDTO;

public class StoreItemsViewImpl extends ViewImpl implements StoreItemsView {

	private final Widget widget;
	private final StoreItemsViewUiBinder uiBinder = GWT.create(StoreItemsViewUiBinder.class);
	private final StoreKeeperConstants storeKeeperConstants = GWT.create(StoreKeeperConstants.class);
	@UiField
	Label status;
	
	@UiField
	DataGrid<ProductDTO> dataGrid;
	
	@UiField
	SimplePager pager;
	
	interface StoreItemsViewUiBinder extends UiBinder<Widget, StoreItemsViewImpl> {

	}

	public StoreItemsViewImpl() {
		widget = uiBinder.createAndBindUi(this);
		pager.setDisplay(dataGrid);
		dataProvider.addDataDisplay(dataGrid);
	}
	
	private ListDataProvider<ProductDTO> dataProvider = new ListDataProvider<ProductDTO>();

	@UiFactory DataGrid<ProductDTO> createDataGrid(){
		DataGrid<ProductDTO> dataGrid = new DataGrid<ProductDTO>(30, ProductDTO.KEY_PROVIDER);
		dataGrid.setWidth("100%");
		dataGrid.setHeight("400px");
		dataGrid.setEmptyTableWidget(new Label(storeKeeperConstants.emptyTableLabel()));
		ListHandler<ProductDTO> sortHandler = new ListHandler<ProductDTO>(dataProvider.getList());
		dataGrid.addColumnSortHandler(sortHandler);
	    initializeColumns(dataGrid,sortHandler);
	    return dataGrid;
	}
	
	@UiFactory SimplePager createPager(){
		SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
	    return new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
	}
	
	public void initializeColumns(DataGrid<ProductDTO> dataGrid,ListHandler<ProductDTO> sortHandler){
		Column<ProductDTO, String> productNameColumn = new Column<ProductDTO, String>(new TextCell()) {

			@Override
			public String getValue(ProductDTO object) {
				return object.getName();
			}
		};
		productNameColumn.setSortable(true);
		sortHandler.setComparator(productNameColumn, new Comparator<ProductDTO>() {
			
			@Override
			public int compare(ProductDTO o1, ProductDTO o2) {
				return o1.getName().compareTo(o2.getName());
			}
		});
		
		Column<ProductDTO,String> productCodeColumn = new Column<ProductDTO, String>(new TextCell()) {
			@Override
			public String getValue(ProductDTO object){
				return object.getCode();
			}
		
		};
		productCodeColumn.setSortable(true);
		sortHandler.setComparator(productCodeColumn, new Comparator<ProductDTO>() {
			@Override
			public int compare(ProductDTO o1,ProductDTO o2){
				return o1.getCode().compareTo(o2.getCode());
			}
		});
		
		Column<ProductDTO,String> productShowDetails = new Column<ProductDTO,String>(new ButtonCell()){
			@Override
			public String getValue(ProductDTO object) {
				return storeKeeperConstants.productShowDetails();
			}
		};

		Column<ProductDTO,String> productAddToCart = new Column<ProductDTO,String>(new ButtonCell()){
			@Override
			public String getValue(ProductDTO object) {
				return storeKeeperConstants.productAddToCart();
			}
			
		};
		dataGrid.addColumn(productNameColumn,storeKeeperConstants.productName());
		dataGrid.addColumn(productCodeColumn,storeKeeperConstants.productCode());
		dataGrid.addColumn(productShowDetails);
		dataGrid.addColumn(productAddToCart);
		
		productShowDetails.setFieldUpdater(new FieldUpdater<ProductDTO, String>() {
			
			@Override
			public void update(int index, ProductDTO object, String value) {
				placeChangeHandler.changePlace(NameTokens.PRODUCT_DETAIL, "id", object.getId().toString());
			}
		});
	}

	@Override
	public Widget asWidget() {
		return this.widget;
	}

	@Override
	public void addProducts(List<ProductDTO> productItr) {
		dataProvider.getList().clear();
		dataProvider.getList().addAll(productItr);
		dataProvider.refresh();
	}
	
	class EachItemDisplayClickHandler implements ClickHandler{
		
		private Long productId;
		public EachItemDisplayClickHandler(Long productId){
			this.productId = productId;
		}
		
		@Override
		public void onClick(ClickEvent event) {
			placeChangeHandler.changePlace(NameTokens.PRODUCT_DETAIL, "id", productId.toString());
		}
		
	}

	@Override
	public void setStatus(String status) {
		this.status.setText(status);
	}
	
	private PlaceChangeHandler placeChangeHandler;
	
	@Override
	public void setPlaceChangeHandler(PlaceChangeHandler placeChangeHandler){
		this.placeChangeHandler=placeChangeHandler;
	}
	
	

}
